﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Xml;

public partial class MaterialBill : System.Web.UI.Page
{
    readonly static string SMS_ONLINE_DB = ConfigurationManager.ConnectionStrings["SMSOnlineDB"].ConnectionString;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
        HttpContext.Current.Response.ClearContent();
        if (string.IsNullOrEmpty(HttpContext.Current.Request["StoreID"]) || string.IsNullOrEmpty(HttpContext.Current.Request["MaterialVersionNumber"]))
        {
            HttpContext.Current.Response.AddHeader("Result", "StoreID or MaterialVersionNumber is NullOrEmpty ");
        }
        else
        {
            try
            {

                using (SqlConnection con = new SqlConnection(SMS_ONLINE_DB))
                {
                    using (SqlCommand com = new SqlCommand())
                    {
                        com.Connection = con;
                        com.CommandType = CommandType.StoredProcedure;
                        com.CommandText = "dbo.usp_GetMaterials";
                        SqlParameter[] param = new SqlParameter[3];
                        param[0] = new SqlParameter("@StoreID", HttpContext.Current.Request["StoreID"]);//来自餐厅端的StoreID
                        param[1] = new SqlParameter("@Ver_Num", HttpContext.Current.Request["MaterialVersionNumber"]); //来自餐厅端的Ver_num
                        param[2] = new SqlParameter("@Final_Ver_Num_Out", SqlDbType.Int);//根据StoreID返回数据库内最大的Ver_Num
                        param[2].Direction = ParameterDirection.Output;
                        foreach (SqlParameter P in param)
                        {
                            com.Parameters.Add(P);
                        }
                        con.Open();
                        int ii = com.ExecuteNonQuery();
                        if (((int)param[2].Value) == 0) //没有最新的物料 Final_Ver_Num_Out>0
                        {
                            HttpContext.Current.Response.AddHeader("Result", "have no any material recently!");//没有最新的物料
                        }
                        else
                        {
                            HttpContext.Current.Response.ContentType = "text/xml";
                            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                            xmlWriterSettings.Indent = true;
                            using (XmlWriter xmlWriter = XmlWriter.Create(HttpContext.Current.Response.OutputStream, xmlWriterSettings))
                            {
                                xmlWriter.WriteStartElement("Root");
                                xmlWriter.WriteAttributeString("Final_Ver_Num", param[2].Value.ToString());
                                SqlDataReader dr = com.ExecuteReader();
                                while (dr.Read())
                                {
                                    xmlWriter.WriteStartElement("Material");
                                    xmlWriter.WriteAttributeString("MID", dr["MaterialID"].ToString());
                                    xmlWriter.WriteAttributeString("MName", dr["MaterialName"].ToString());
                                    xmlWriter.WriteElementString("Major_Category", dr["ParentCategoryName"].ToString());
                                    xmlWriter.WriteElementString("Sub_Category", dr["SubCategoryName"].ToString());
                                    xmlWriter.WriteElementString("Packing_Spec", dr["Spec"].ToString());
                                    xmlWriter.WriteElementString("Base_Unit", dr["BaseUnit"].ToString());
                                    xmlWriter.WriteElementString("Order_Unit", dr["OrderUnit"].ToString());
                                    xmlWriter.WriteElementString("Unit_Conv_Value", dr["OrderNumber"].ToString());
                                    xmlWriter.WriteElementString("MaxValue", dr["MaxValue"].ToString());
                                    xmlWriter.WriteElementString("MinValue", dr["MinValue"].ToString());
                                    xmlWriter.WriteElementString("Order_Cycle", dr["Order_Cycle"].ToString());
                                    xmlWriter.WriteElementString("Stocktaking_Cycle", dr["Stocktaking_Cycle"].ToString());
                                    xmlWriter.WriteElementString("Description", dr["Description"].ToString());
                                    xmlWriter.WriteElementString("Status", dr["Status"].ToString());
                                    xmlWriter.WriteElementString("DeliveryType", dr["DeliveryType"].ToString());
                                    xmlWriter.WriteElementString("StorageType", dr["StorageType"].ToString());
                                    xmlWriter.WriteEndElement();//Material                
                                }
                                dr.Close();
                                xmlWriter.WriteEndElement();//root

                                xmlWriter.Flush();
                                xmlWriter.Close();
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {

                HttpContext.Current.Response.AddHeader("Result", ex.ToString());
            }
        }
        HttpContext.Current.Response.End();
    }
}
